package com.shxy.service;

import com.shxy.dao.INodeDao;
import com.shxy.dao.IMaterialCategoryDao;
import com.shxy.pojo.Node;
import com.shxy.pojo.MaterialCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService implements ICategoryService {
    @Autowired
    private INodeDao nodeDao;
    @Autowired
    private IMaterialCategoryDao materialCategoryDao;

    @Override
    public Node getNodeById(int id) {
        return nodeDao.getById(id);
    }

    @Override
    public boolean existNodeId(int nodeId) {
        return nodeDao.existsById(nodeId);
    }

    @Override
    public boolean existNode(Node node) {
        //如果是根节点
        if(node.getLevel() == 0){
            return nodeDao.getByLevelAndName(0, node.getName()) != null;
        }
        return nodeDao.existsByParent_IdAndName(node.getParent().getId(), node.getName());
    }

    @Override
    public Node addNode(Node node) {
        //如果该节点存在
        if(existNode(node)){
            return nodeDao.getByParent_IdAndName(node.getParent().getId(), node.getName());
        }
        return nodeDao.save(node);
    }

    @Override
    public boolean deleteNode(int nodeId) {
        if(existNodeId(nodeId)){
            nodeDao.deleteById(nodeId);
            return true;
        }
        return false;
    }

    @Override
    public Node updateNode(Node catalog) {
        return nodeDao.save(catalog);
    }

    @Override
    public MaterialCategory addMaterialCategory(MaterialCategory materialCategory) {
        return materialCategoryDao.save(materialCategory);
    }

    @Override
    public boolean deleteMaterialCategory(String materialCategoryId) {
        materialCategoryDao.deleteById(materialCategoryId);
        return true;
    }

    @Override
    public MaterialCategory updateMaterialCategory(MaterialCategory materialCategory) {
        return materialCategoryDao.save(materialCategory);
    }

    @Override
    public List<Node> getChildrenList(int parentId, boolean children) {
        List<Node> list = new ArrayList<>();
        List<Node> catalogs = nodeDao.getAllByParent_Id(parentId);
        if(children){
            for (Node c: catalogs) {
                list.add(c);
                list.addAll(getChildrenList(c.getId(), true));
            }
        }
        return list;
    }

    @Override
    public int clearMaterialsOfCatalog(int catalogId, boolean children) {
        List<Node> catalogs = getChildrenList(catalogId, children);
        int n = 0;
        for (Node c: catalogs) {
            n += materialCategoryDao.deleteAllByNode_Id(catalogId);
        }
        return n;
    }

    @Override
    public boolean setMaterialCategoryIndex(String materialCategoryId, int index) {
        MaterialCategory mc = materialCategoryDao.getById(materialCategoryId);
        if(mc != null){
            mc.setIdx(index);
            updateMaterialCategory(mc);
            return true;
        }
        return false;
    }
}
